package com.example.backend.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend.api.common.Result;
import com.example.backend.api.entity.Employee;
import com.example.backend.api.mapper.EmployeeMapper;
import com.example.backend.api.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public Result getAll(Integer deptId) {
        if (deptId == null) {
            // select all
            return Result.success(employeeMapper.selectList(null));
        }
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        return Result.success(employeeMapper.selectList(queryWrapper));
    }

    public Result updateUser(Integer userId, String userName, Date birth, String tel, String email, Integer deptId, String gender, String address, String password, String nation, String education, String qq) {

        UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        Employee employee = new Employee();
        employee.setUserId(userId);
        if (userName!=null) {
            updateWrapper.set("name", userName);
            employee.setName(userName);
        }
        if (birth != null) {
            updateWrapper.set("birth", birth);
            employee.setBirth(birth);
        }
        if (tel!=null) {
            updateWrapper.set("tel", tel);
            employee.setTel(tel);
        }
        if (email!=null) {
            updateWrapper.set("email", email);
            employee.setEmail(email);
        }
        if (deptId != null) {
            updateWrapper.set("dept_id", deptId);
            employee.setDeptId(String.valueOf(deptId));
        }
        if (gender!=null) {
            updateWrapper.set("gender",gender);
            employee.setGender(gender);
        }
        if (address!=null) {
            updateWrapper.set("address", address);
            employee.setAddress(address);
        }
        if (password!=null) {
            updateWrapper.set("password", password);
            employee.setPassword(password);
        }
        if (nation!=null) {
            updateWrapper.set("nation", nation);
            employee.setNation(nation);
        }
        if (education!=null) {
            updateWrapper.set("education", education);
            employee.setEducation(education);
        }
        if (qq!=null) {
            updateWrapper.set("qq", qq);
            employee.setQq(qq);
        }
        int code = employeeMapper.update(employee, updateWrapper);
        if (code == 1) {
            return Result.success("update success");
        }
        return Result.fail("update fail");
    }

    public Result deleteUser(Integer userId) {
        UpdateWrapper<Employee> employeeUpdateWrapper = new UpdateWrapper<>();
        employeeUpdateWrapper.eq("user_id", userId);
        int code = employeeMapper.delete(employeeUpdateWrapper);
        if (code == 1) {
            return Result.success("delete success");
        }
        return Result.fail("delete fail");
    }

    public Result login(Integer userId, String password) {

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("password", password);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        if ( employee != null) {
            return Result.success(employee);
        }
        return Result.fail("login fail");

    }

    public Result getEmployeeByDept(Integer deptId) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        return Result.success(employeeMapper.selectList(queryWrapper));
    }

    public Result addEmploy(String username, String birth, Integer deptId,
                            String tel, String qq, String email, String gender, String address, String password, String nation, String education,
                            Integer userId
                            ) throws ParseException {

        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Employee employee1 = employeeMapper.selectOne(queryWrapper);
        if (employee1 != null) {
            return Result.fail("user id already exist");
        }

        Employee employee = new Employee();
        employee.setUserId(userId);
        employee.setName(username);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(birth);
        employee.setBirth(date);
        employee.setDeptId(String.valueOf(deptId));
        employee.setTel(tel);
        employee.setQq(qq);
        employee.setEmail(email);
        employee.setAddress(address);
        employee.setNation(nation);
        employee.setEducation(education);
        employee.setGender(gender);
        employee.setPassword(password);
        int code = employeeMapper.insert(employee);
        if (code == 1) {
            return Result.success("add success");
        }else{
            return Result.fail("add fail");
        }
    }
}
